package com.keosimage.action;

import java.io.File;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.org.json.JSONObject;
import weibo4j.util.WeiboConfig;

import com.keosimage.common.KeosImageBaseAction;
import com.keosimage.common.KeosImageConstants;
import com.keosimage.common.KeosImagePostParameter;
import com.keosimage.common.KeosImageUtils;
import com.keosimage.entity.WeiboContent;
import com.keosimage.entity.WeiboContentResult;

public class SinaWeiboAction extends KeosImageBaseAction {

	static Logger log = Logger.getLogger(SinaWeiboAction.class.getName());

	private static final long serialVersionUID = 5600708839515959681L;

	private WeiboContent weibocontent;
	private WeiboContentResult weibocontentresult;

	public WeiboContent getWeibocontent() {
		return weibocontent;
	}

	public void setWeibocontent(WeiboContent weibocontent) {
		this.weibocontent = weibocontent;
	}

	public WeiboContentResult getWeibocontentresult() {
		return weibocontentresult;
	}

	public void setWeibocontentresult(WeiboContentResult weibocontentresult) {
		this.weibocontentresult = weibocontentresult;
	}

	public String topost() throws Exception {
		return SUCCESS;
	}

	public String post() throws Exception {
		Weibo weibo = new Weibo();
		AccessToken accesstoken = (AccessToken) this.session
				.getAttribute(KeosImageConstants.ACCESS_TOKEN);
		weibo.setToken(accesstoken.getAccessToken());

		File pic = weibocontent.getPic();

		KeosImagePostParameter keosImagePostParameter = new KeosImagePostParameter();
		if (KeosImageUtils.isEmpty(pic)) {
			keosImagePostParameter.add("status",
					KeosImageUtils.addNullParam(weibocontent.getContent()));
		} else {
			keosImagePostParameter.add("status", URLEncoder.encode(
					KeosImageUtils.addNullParam(weibocontent.getContent()),
					"UTF-8"));
		}
		if (!KeosImageUtils.isEmpty(weibocontent.getLocallat())
				&& !KeosImageUtils.isEmpty(weibocontent.getLocallong())) {
			keosImagePostParameter.add("lat",
					KeosImageUtils.addNullParam(weibocontent.getLocallat()));

			keosImagePostParameter.add("long",
					KeosImageUtils.addNullParam(weibocontent.getLocallong()));
		}

		JSONObject result = null;
		if (!KeosImageUtils.isEmpty(pic)) {
			result = Weibo.client.multPartURL("pic",
					WeiboConfig.getValue("baseURL") + "statuses/upload.json",
					keosImagePostParameter.PostParameters(), pic, true)
					.asJSONObject();
		} else {
			result = Weibo.client.post(
					WeiboConfig.getValue("baseURL") + "statuses/update.json",
					keosImagePostParameter.PostParameters()).asJSONObject();
		}
		log.debug(result);
		if (KeosImageUtils.isSuccess(result)) {
			weibocontentresult = new WeiboContentResult();
			weibocontentresult.setCreated_at(result.getString("created_at"));
			weibocontentresult.setId(result.getString("id"));
			weibocontentresult.setText(result.getString("text"));
			weibocontentresult.setSource(result.getString("source"));
			weibocontentresult.setGeo(result.getString("geo"));
		}

		return SUCCESS;
	}
}
